package com.gxa.service.community.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.UUID;
import com.gxa.dto.communitydto.UserApplyDto;
import com.gxa.dto.communitydto.UserApplySearchDto;
import com.gxa.entity.community.UserApply;
import com.gxa.entity.community.UserList;
import com.gxa.mapper.community.UserApplyMapper;
import com.gxa.service.community.UserApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Service
public class UserApplyServiceImpl extends ServiceImpl<UserApplyMapper, UserApply> implements UserApplyService {

    @Autowired
    private UserApplyMapper userApplyMapper;

    @Override
    public PageInfo<UserApply> queryAll(UserApplySearchDto userApplySearchDto,Integer pageNum,Integer pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<UserApply> userApplies = userApplyMapper.queryAll(userApplySearchDto);
        PageInfo<UserApply> userApplyDtoPageInfo = new PageInfo<>(userApplies);
        return userApplyDtoPageInfo;
    }

    @Override
    public void add(UserApply userApply) {
        userApplyMapper.insertApplyUser(userApply);
    }

    @Override
    public void delete(Integer id) {
        userApplyMapper.deleteById(id);
    }

    @Override
    public void updateState(UserApply userApply) {
        System.out.println(userApply.toString());
        UserApply userApply1 = userApply.setApplyState(Math.abs(userApply.getApplyState() - 1));
        System.out.println(userApply1.toString());
//        userApplyMapper.updateById(userApply1);
        userApplyMapper.updateState(userApply.getId());
        if (userApply1.getApplyState() ==1){
            String user_num = UUID.getShortUuid();
            UserList userList = new UserList();
            userList.setUserNum(user_num)
                    .setUserPwd(userApply1.getApplyPwd())
                    .setUserName(userApply1.getApplyName())
                    .setUserPhone(userApply1.getPhone())
                    .setSex(userApply1.getSex())
                    .setUserAccount(userApply1.getPhone())
                    .setIdNumber(userApply1.getIdNumber())
                    .setHouseNum(userApply1.getHouse().getHouseNum())
                    .setOwnerType(userApply1.getOwnerType());

            userApplyMapper.insertToUserList(userList);
        }
    }


}
